/**
 * @program: PavtFirmware
 * @description:
 * @author: zyc
 * @create: 2018-06-13 16:47
 **/
package com.zjpavt.client.command.entity;

import com.zjpavt.client.enums.LoopStatusEnum;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
@Slf4j
public abstract class AbstractCommand implements ICommand {
    protected int[] commandArray;

    /**
     * 根据字符串初始化指令
     * @param commandStr
     */
    public AbstractCommand(String commandStr) {
        int length = commandStr.length();
        this.commandArray = new int[length];
        for(int i=0; i<length; i++) {
            this.commandArray[i] = Character.getNumericValue(commandStr.charAt(i));
        }
    }

    public AbstractCommand(int[] commandArray) {
        int length = commandArray.length;
        this.commandArray = Arrays.copyOf(commandArray,length);

    }

    public AbstractCommand(int[] commandArray,int length) {
        if (length > commandArray.length) {
            //类似Arrays.copy  先建立了合适位数的数组 然后对于可填充的位置进行补0.
            this.commandArray = new int[length];
            System.arraycopy(commandArray, 0, this.commandArray, 0,
                             Math.min(commandArray .length, length));
            log.error("command lenght is shorter than wanted:" + length + " but " + commandArray.length);
        } else if (length < commandArray.length) {
            //可能arrayLength 比我们需要的length 更大
            this.commandArray = Arrays.copyOf(commandArray, length);
        } else {
            this.commandArray = Arrays.copyOf(commandArray, length);
        }
    }

    public AbstractCommand() {
    }
    @Override
    public int[] getCommand() {
        return Arrays.copyOf(commandArray,commandArray.length);
    }

    @Override
    public int[] getCommand512() {
        int[] result = new int[commandArray.length];
        for (int i = 0; i < commandArray.length; i++) {
            result[i] = commandArray[i] == 0 ? 255 : 0xf;
        }
        return result;
    }

    @Override
    public String getCommandString() {
        StringBuilder commandStrBuilder = new StringBuilder();
        for (int state : commandArray) {
            commandStrBuilder.append(state);
        }
        return commandStrBuilder.toString();
    }

    @Override
    public int getLength() {
        return this.commandArray.length;
    }

    @Override
    public int getCommand(int index) {
        if (isIndexOurOfBound(index)) {
            return 1;
        }
        return commandArray[index];
    }

    protected boolean isIndexOurOfBound(int index) {
        if (index > getLength() || index < 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean isOnOrOffCommand() {
        int[] copyCommand =  getCommand();
        for (int commandBit : copyCommand) {
            if (!isOnOrOff(commandBit)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean isAutoOrManualCommand() {
        for (int state : this.commandArray) {
            if (!LoopStatusEnum.isManualOrAutoStatus(state)) {
                return false;
            }
        }
        return true;
    }

    protected boolean isOnOrOff(int commandBit) {
        return commandBit == 0 || commandBit == 1;
    }

    protected int[] string2IntArray(String command) {
        char[] commandCharArr = command.toCharArray();
        int[] result = new int[command.length()];
        for (int i=0; i<command.length(); i++ ) {
            result[i] = Character.getNumericValue(commandCharArr[i]);
        }
        return result;
    }

    protected String intArr2String(int[] commandArr) {
        StringBuilder commandBuilder = new StringBuilder();
        for (int temp : commandArr) {
            commandBuilder.append(temp);
        }
        return commandBuilder.toString();
    }
}
